package com.rjhch.tonglink.client.jms.tutorial;


import javax.jms.*; // The JMS classes

import javax.naming.*; // The JNDI classes

import javax.naming.directory.*;
import java.io.*;
import java.util.*;

class PubSub {
    public static final String tcf = "tongtech.jms.jndi.JmsContextFactory";/* initial context factory*/
    public static final String localURL = "tlqlocal://localhost/qcu1";
    public static final String remoteURL = "tlkq://127.0.0.1:10025";
    public static final String localFactory = "LocalTopicConnectionFactory";
    public static final String remoteFactory = "RemoteTopicConnectionFactory";

    public static final String url = "tlqlocal://localhost/qcu1";
    // Pub/Sub objects used by this program

    public static void main(String args[]) {
    String url = null;
    String factory = null;
        Properties prop = new Properties();
		//Read command line arguments
		for (int i = 0; i < args.length; i++) {
			if (args[i].startsWith("-")) {
				if (args[i].equals("-protocol")) {
					String protocol = args[++i];
					if (protocol.equals("local")) {
						url = localURL;
						factory = localFactory;
					} else {
						url = remoteURL;
						factory = remoteFactory;
					}
				} else if ( (args[i].equals("-h")) || (args[i].equals("-?")) ) {
					usage("HELP: ");
					return;
				}  else {
					usage("Unknown option: " + args[i++]);
					return;
				}
			} else {
				//done with options
				usage("Extra arguments detected. first is: " + args[i]);
				return;
			}
		}
		        prop.put("java.naming.factory.initial", tcf);
        prop.put("java.naming.provider.url", url);
        /*初始化上下文*/
        TopicConnectionFactory tcf = null;
        Context ictx = null;
        Topic topic = null;
        
        try {
            ictx = new InitialContext(prop);
        } catch (NamingException ne) {
            System.out.println("error:" + ne);
            System.exit(-1);
        }
        try {
            /*获得TopicConnectionFactory*/
            tcf = (TopicConnectionFactory) ictx.lookup(factory);
            /*获得Topic*/
            topic = (Topic) ictx.lookup("MyTopic");
        } catch (NamingException ne) {
            System.out.println("error:" + ne);
            System.exit(-1);
        }
        /*关闭上下文*/
        try {
            ictx.close();
        } catch (NamingException ne) {
        }
        try {
            /*创建TopicConnection*/
            TopicConnection conn = tcf.createTopicConnection();
            /*启动连接*/
        //    conn.start();
            /*创建TopicSession*/
            TopicSession session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            /*创建TopicPublisher*/
            TopicPublisher pubA = session.createPublisher(topic);
            /*创建TopicSubscriber*/
            TopicSubscriber subA = session.createSubscriber(topic);
            conn.start();
            /*创建欲发布的消息*/
            TextMessage message1 = session.createTextMessage();
            message1.setText("how are you?");
            pubA.publish(message1);

            /*接收发布的消息*/
            TextMessage message2 = (TextMessage) subA.receive();
            System.out.println("Message Text = " + message2.getText());

            // 关闭使用完毕的对象
            pubA.close();
            subA.close();
            session.close();
            conn.close();
        } catch (JMSException je) {
            System.out.println("ERROR: " + je);
            System.out.println("LinkedException: " + je.getLinkedException());
            System.exit(-1);
        }
    }/*main over*/

	public static void usage(String error) {
		
		if (error != null) {
			System.out.println(error);
		}
		System.out.println("Usage: PubSub -protocol <local|remote>");
		System.out.println("\tprotocol: protocol type, local or remote");
	}
}/*class over*/
